package com.sky.loveshop.controller;

import com.sky.loveshop.manager.MqttClientManager;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author xcy
 * @version V1.0
 * @description
 * @date 2024/9/6 21:32
 */
@RestController
@Slf4j
public class SendController {

    @Resource
    private MqttClientManager mqttClientManager;

    // http://127.0.0.1:8099/sendMessage?topic=abc/
    @RequestMapping("/sendMessage")
    public String sendMessage(String topic) {
        try {
            MqttMessage mqttMessage = new MqttMessage("{\"addr\":01,\"cmd\":\"set\",\"y\":[1,0],\"count\":[0,0],\"delay\":[5,0]}".getBytes());
            mqttClientManager.getMqttClientById("mqttx_561b5b53").publish(topic, mqttMessage);
            return "发送成功";
        } catch (Exception e) {
            log.error("sendMessage 发送失败", e);
            e.printStackTrace();
            return "发送失败";
        }
    }
}